<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\mongodb\Database;

/**
 * This is the model class for table "mops_coupon".
 *
 * @property int $id
 * @property string $name
 * @property string $code 优惠券号
 * @property int $catalog 分类
 * @property int $created_at 创建时间
 * @property int $updated_at 创建时间
 * @property int $expire_at 过期时间
 * @property int $origin_num 初始值
 * @property int $use_num 领取数
 * @property string $catalog_mark_id 分类标识ID
 * @property number $consumption_amount
 * @property number $discount_amount
 * @property int $type  类型
 * @property string $url 跳转地址
 * @property string $describe 描述
 * @property int $origin_id
 * @property int $status
 * @property string $password
 * @property string $desc 扩展字段
 */
class Coupon extends \yii\db\ActiveRecord
{
    public $expire_time;

    const STATUS_VALID = 1;
    const STATUS_INVALID = 0;



    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%coupon}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['origin_id','origin_num','use_num','catalog'], 'integer'],
            [['name','catalog_mark_id','code','desc'], 'string'],
        ];
    }


    public static function getStatusList()
    {
        return [
            self::STATUS_INVALID => '无效',
            self::STATUS_VALID => '有效'
        ];
    }


    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'origin_id' => '原始ID',
            'code' => '券号',
            'origin_num' => '原始数',
            'use_num' => '领取数',
            'catalog_mark_id'=>'分类标识ID',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'expire_at' => '过期时间',
            'name' => '名称',
            'consumption_amount' => '消费金额',
            'discount_amount' => '折扣金额',
            'password' => '密码',
            'status' => '状态',
            'desc' => '扩展字段'
        ];
    }

    public function behaviors()
    {
        return [
            TimestampBehavior::className()
        ];
    }

    public function getCouponCategory()
    {
        return $this->hasOne(CouponCategory::className(), ['mark_id' => 'catalog_mark_id']);
    }



    public function objToArr()
    {
        return [
            'id' => $this->id,
            'origin_id' => $this->origin_id,
            'code' => $this->code,
            'origin_num' => $this->origin_num,
            'use_num' => $this->use_num,
            'catalog'=>$this->catalog,
            'catalog_mark_id'=>$this->catalog_mark_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'expire_at' => $this->expire_at,
            'name' => $this->name,
            'consumption_amount' => $this->consumption_amount,
            'discount_amount' => $this->discount_amount,
            'password' => $this->password,
            'status' => $this->status,
            'desc' => $this->desc,
        ];
    }

    public function afterDelete()
    {
        $collection = self::getCollection();
        $collection->remove(['id' => $this->id]);
        return true;
    }

    public function afterSave($insert, $changedAttributes)
    {
        $collection = self::getCollection();
        $find = $collection->findOne(['id' => $this->id]);
        if ($find) {
            $collection->update(['id' => $this->id], $this->objToArr());
        } else {
            $collection->insert($this->objToArr());
        }
        return true;
    }


    public static function getCollection()
    {
        /** @var Database $database */
        $database = Yii::$app->mongo->getDatabase();
        return $collection = $database->getCollection("coupon");
    }
}
